﻿using System;

namespace TI_BLE_HCI_ClientLib.DataEntities.Advertising
{
    public class AdvertisingAndScanResponseData
    {
        private AdvertisingDataStructure[] _adverstisingDataStructures = new AdvertisingDataStructure[0];

        public AdvertisingDataStructure[] AdvertisingDataStructures
        {
            get { return _adverstisingDataStructures; }
            set { _adverstisingDataStructures = value; }
        }

        public void addAdvertisingDataStructure(AdvertisingDataStructure advertisingDataStructure)
        {
            AdvertisingDataStructure[] advertisingDataStructures = new AdvertisingDataStructure[_adverstisingDataStructures.Length + 1];

            Array.Copy(_adverstisingDataStructures, 0, advertisingDataStructures, 0, _adverstisingDataStructures.Length);
            advertisingDataStructures[advertisingDataStructures.Length - 1] = advertisingDataStructure;

            _adverstisingDataStructures = advertisingDataStructures;
        }

        public byte[] ToByteArray()
        {
            byte idx = 0;
            byte[] buf = new byte[31];

            foreach(var adStructure in _adverstisingDataStructures)
            {
                var adbuf = adStructure.ToByteArray();

                Array.Copy(adbuf, 0, buf, idx, adbuf.Length);

                idx += (byte)adbuf.Length;
            }

            byte[] clippedBuf = new byte[idx];

            Array.Copy(buf, clippedBuf, clippedBuf.Length);

            return clippedBuf;
        }
    }
}